<?php
/**
 * @title 72. 编辑距离
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param String $word1
     * @param String $word2
     * @return Integer
     */
    function minDistance($word1, $word2) {
        /**
         * @since 2024-01-12 字符串的长度
         */
        $this->len1 = strlen($word1);
        $this->len2 = strlen($word2);

        /**
         * @since 2024-01-12 全局变量
         */
        $this->word1 = $word1;
        $this->word2 = $word2;

        /**
         * @since 2024-01-12 减少重复遍历
         */
        $this->dp = [];

        /**
         * @return Integer
         */
        return $this->FindMinDistance(0, 0);
    }

    /**
     * @title 最短距离
     * @author start2004
     * @since 2024-01-12 Created
     *
     * @param Integer $p1
     * @param Integer $p2
     */
    function FindMinDistance($p1, $p2){
        /**
         * @since 2024-01-12 是否命中缓存
         */
        $pKey = "{$p1}-{$p2}";
        if(isset($this->dp[$pKey])){
            return $this->dp[$pKey];
        } else {}

        /**
         * @since 2024-01-12 最少操作数
         */
        $minCount = PHP_INT_MAX;

        /**
         * @since 2024-01-12 指针在范围内
         */
        if($p1 < $this->len1 AND $p2 < $this->len2){
            /**
             * @since 2024-01-12 word1[p1] == word2[p2]
             */
            if($this->word1[$p1] == $this->word2[$p2]){
                $minCount = $this->FindMinDistance($p1+1, $p2+1);
            } else {
                /**
                 * @since 2024-01-12 替换
                 */
                $minCount = min($minCount, 1+$this->FindMinDistance($p1+1, $p2+1));

                /**
                 * @since 2024-01-12 删除
                 *
                 * a b c d e
                 * e a b c d
                 * 判断长度，决定给不给删，会有问题
                 */
                $minCount = min($minCount, 1+$this->FindMinDistance($p1+1, $p2));

                /**
                 * @since 2024-01-12 添加
                 *
                 * a b c d e
                 * e a b c d
                 */
                $minCount = min($minCount, 1+$this->FindMinDistance($p1, $p2+1));
            }
        } else {
            /**
             * @since 2024-01-12 指针超出范围
             * @since 2024-01-12 word1多出来的字符，全部删除
             */
            if($p1 < $this->len1){
                $minCount = $this->len1 - $p1;
            } elseif($p2 < $this->len2){
                /**
                 * @since 2024-01-12 word1不够的字符，全部添加
                 */
                $minCount = $this->len2 - $p2;
            } else {
                /**
                 * @since 2024-01-12 字符串全部匹配完
                 */
                $minCount = 0;
            }
        }

        /**
         * @return Integer
         */
        return $this->dp[$pKey] = $minCount;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.12_16.php
 */
$datas = [
    ["horse", "ros"], // 3
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minDistance(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
